Skip to content

Latest commit

 

History

History
223 lines (181 loc) · 3.78 KB

5. Conditional Statements - C++.md

File metadata and controls

223 lines (181 loc) · 3.78 KB

5. Conditional Statements

'if' Statement

if (condition) {
  // block of code to be executed if the condition is true
}

'if - else' Statement

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

'if - else' Statement Alternative - Ternary Operator Statement

variable = (condition) ? expressionTrue : expressionFalse;

Example: 'if-else'

int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}

Example: 'ternary' operator

int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;

'else-if' Statement

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

'switch' Statement

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Example:

int day = 4;
switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
}
// Outputs "Thursday" (day 4)

'goto' Statement

  • It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label.
  • It can be used to transfer control from a deeply nested loop or switch case label.
  • Syntax 1:
goto label;
.
.
.
label:
  • Syntax 2:
label:
.
.
.
goto label;

Example: syntax 1 demonstration

#include <iostream>
using namespace std;


// function to check even or not
void checkEvenOrNot(int num)
{
    if (num % 2 == 0)
        // jump to even
        goto even;
    else
        // jump to odd
        goto odd;


even:
    cout << num << " is even";
    // return if even
    return;
odd:
    cout << num << " is odd";
}


// Driver program to test above function
int main()
{
    int num = 26;
    checkEvenOrNot(num);
    return 0;
}

// Output: 26 is even

Example: syntax 2 demonstration

#include <iostream>
using namespace std;
 
// function to print numbers from 1 to 10
void printNumbers()
{
    int n = 1;
label:
    cout << n << " ";
    n++;
    if (n <= 10)
        goto label;
}
 
// Driver program to test above function
int main()
{
    printNumbers();
    return 0;
}


// Output: 1 2 3 4 5 6 7 8 9 10


Disadvantages of using goto statement:

  • The use of the 'goto' statement is highly discouraged as it makes the program logic very complex.

  • The use of 'goto' makes the task of analysing and verifying the correctness of programs (particularly those involving loops) very difficult.

  • Use of 'goto' can be simply avoided using break and continue statements.